package com.nlscan.machinetest.antutusize;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.view.WindowMetrics;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getScreenSize(this);
    }

    // https://blog.csdn.net/m0_53394296/article/details/134014287

    public static final Point getMaxWinPoint(Context context) {
        Point screenSize = new Point();

        if (context != null) {
            if (Build.VERSION.SDK_INT >= 30) {
                // For Android 11 (API level 30) and above
                WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                //获取最大窗口的度量信息，包括该窗口的边界、大小等
                WindowMetrics maximumWindowMetrics = windowManager.getMaximumWindowMetrics();
                //从 maximumWindowMetrics 中获取窗口的边界，并获取其宽度
                screenSize.x = maximumWindowMetrics.getBounds().width();
                //从 maximumWindowMetrics 中获取窗口的边界，并获取其高度，这里的高度和宽度即为分辨率
                screenSize.y = maximumWindowMetrics.getBounds().height();
            } else {
                // For versions below Android 11
                Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                if (display != null) {
                    display.getRealSize(screenSize);
                }
            }
        }

        return screenSize;
    }


    public static final DisplayMetrics getDisplayMetrics(Context context) {
        if (Build.VERSION.SDK_INT >= 30) {
            return context.getResources().getDisplayMetrics();
            //int densityDPI = context.getResources().getDisplayMetrics().densityDpi;获取屏幕密度（dpi）
            //Log.i(TAG, "densityDPI = "+densityDPI);
        } else {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.getMetrics(displayMetrics);

            return displayMetrics;
        }
    }

    public static final double getScreenSize(Context context) {
        if (context != null) {
            Point point = getMaxWinPoint(context);
            DisplayMetrics metrics= getDisplayMetrics(context);
            Log.d(TAG, "point=" + point);
            Log.d(TAG, "metrics=" + metrics);
//            metrics.xdpi =  metrics.widthPixels > metrics.heightPixels ? 215.88f : 215.0f;
//            metrics.ydpi = metrics.widthPixels > metrics.heightPixels ? 215.0f : 215.88f;
//            metrics.xdpi =  metrics.widthPixels > metrics.heightPixels ? 196.0f : 195.7f;
//            metrics.ydpi = metrics.widthPixels > metrics.heightPixels ? 195.7f : 196.0f;
            metrics.xdpi =  metrics.widthPixels > metrics.heightPixels ? 149.62f : 149.0f;
            metrics.ydpi = metrics.widthPixels > metrics.heightPixels ? 149.0f : 149.62f;

//            metrics.xdpi =  metrics.widthPixels > metrics.heightPixels ? 141.27f : 141.0f;
//            metrics.ydpi = metrics.widthPixels > metrics.heightPixels ? 141.0f : 141.27f;

            double sqrt = Math.sqrt(Math.pow(point.x / metrics.xdpi, 2) + Math.pow(point.y / metrics.ydpi, 2));
            Log.i(TAG, "11111: "+sqrt);
            double size = new BigDecimal(String.valueOf(sqrt)).setScale(2, 4).doubleValue();
            Log.i(TAG, "getScreenSize   : "+size);
            return size;
        }
        return 0.0d;
    }

}